package com.zsh.myserver.api.v1;

import com.zsh.myserver.model.Banner;
import com.zsh.myserver.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * Copyright (C), 2020-2020
 * Author: Zhoushaohua
 * Date: 2020/4/13 22:47
 * Description: Banner控制器
 */
@RequestMapping("/banner")
@RestController
@Validated
public class BannerController {
    @Autowired
    private BannerService bannerService;

    /**
     * 实体类对象和json格式的转换
     * <p>
     * 将user对象转换成json格式的字符串
     * String userJson = JSON.toJSONString(user);
     * 将userJson的json格式转换成Object格式
     * <p>
     * JSONObject user = JSONObject.parseObject(userJson);
     * 获取Object中的Name字段的值
     * <p>
     * user.getString("Name")；
     *
     * @param name
     */
    @GetMapping("/name/{name}")
    public Banner getByName(@PathVariable @NotBlank String name) {
        //1、需要先校验name的可用性
        //2、在使用该name进行操作
        Banner banner = bannerService.getByName(name);
        System.out.println("banner is null ? " + (banner == null));
        //String bannerJson = JSON.toJSONString(banner);
        //System.out.println(bannerJson);
        return banner;
    }

}
